/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * 
 *
 * Created on 18-mei-2011, 0:29:51
 */
package View;
/**
 *
 * @author Chong & Ali
 */
import Model.*;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JOptionPane;

public class Game extends JPanel{
    
    public static final int PIXEL = Level.PIXEL;   
    private Level level;  
    private int currentLevel = 1;
    private final int LAST_LEVEL = 3;    

        
    public Game() {
        initComponents();
        level = new Level(currentLevel);
        addKeyListener(new Key4Listener());
    }
    
    public Level nextLevel(){
        if (Level.isCompleted() == true && currentLevel == LAST_LEVEL){
            JOptionPane.showMessageDialog(null, "Congratulations, you have completed the game!");
        }
        else if (Level.isCompleted() == true){
             JOptionPane.showMessageDialog(null, "Level completed!");
             currentLevel++;          
             level = new Level(currentLevel);
             
        }
        repaint();
        return level;
    }
    
    @Override
    public void paint(Graphics g){
        super.paint(g);
        level.paintLevel(g); 
    }
   
    class Key4Listener implements KeyListener {
        @Override
        public void keyTyped(KeyEvent e) {
            
        }
        @Override
        public void keyReleased(KeyEvent e) {
            
        }
        @Override
        public void keyPressed(KeyEvent e) {
            
            int pressedKey = e.getKeyCode();
            if (pressedKey == KeyEvent.VK_UP) {
                level.getPlayer().move("north", level.getlocations());
            } 
            else if (pressedKey == KeyEvent.VK_RIGHT) {  
                level.getPlayer().move("east", level.getlocations());
            } 
            else if (pressedKey == KeyEvent.VK_DOWN) {
                level.getPlayer().move("south", level.getlocations());
            } 
            else if (pressedKey == KeyEvent.VK_LEFT) {  
                level.getPlayer().move("west", level.getlocations());
            }
            
            repaint(); 
            nextLevel();
            
            
        } 
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBackground(new java.awt.Color(51, 153, 255));
        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 544, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 358, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
